<?php
/**
 * gon
 */
namespace app\index\model;

use think\Model;

class Common extends Model
{
	// 表名
    protected $name = 'news_comment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;




	/**
	 * 获取一条数据
	 */
	public function fetch($filter, $field = '*')
	{
		$res = $this->where($filter)->field($field)->find();
		$res = $res ? $res->toArray() : [];
		return $res;
	}

	/**
	 * 获取多条数据
	 */
	public function fetchAll($filter, $field = '*', $order = null)
	{
		$filter['comment_id']=0;
		if(is_null($order)){
			$res = $this->where($filter)->field($field.',id as children')->select();
		}else{
			$res = $this->where($filter)->field($field.',id as children')->order($order)->select();
		}
		$res = $res ? $res->toArray() : [];
		return $res;
	}

	/**
	 * 插入数据
	 */
	public function addData($data)
	{
		$res = $this->insert($data);
		return $res;
	}

	/**
	 * 更新数据
	 */
	public function editData($filter, $data)
	{
		$res = $this->where($filter)->update($data);
		return $res;
	}

	/**
	 * 统计数据
	 */
	public function countData($filter, $field)
	{
		$res = $this->where($filter)->count($field);
		return $res;
	}

	/**
	 * 统计总和数据
	 */
	public function sumData($filter, $field)
	{
		$res = $this->where($filter)->sum($field);
		return $res;
	}

	public function getMemberNicknameAttr($value,$data){
       return (new \app\index\model\Members)::get(['member_id'=>$value??0])['nickname']??'';
    }

    public function getMemberAvatarAttr($value,$data){
    	$avatar = (new \app\index\model\Members)::get(['member_id'=>$value??0])['avatar']??'';
    	if(!empty($avatar)){
    		 //获取类型
          	$url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public' . $avatar;
          	$url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $avatar;
          	$avatar = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:$avatar);
    	}else{
    		$url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public/assets/img/avatar.png';
          	$url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public/assets/img/avatar.png';
          	$avatar = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:'');
    	}
       return $avatar;
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return $this->formattime($value);
    }
    
    public function getPsignTextAttr($value,$data){
       return (new \app\index\model\Members)::get(['member_id'=>$value??0])['sign_text']??'';
    }

    public function getPmemberNicknameAttr($value,$data){
       return (new \app\index\model\Members)::get(['member_id'=>$value??0])['nickname']??'';
    }

    public function getPmemberAvatarAttr($value,$data){
    	$avatar = (new \app\index\model\Members)::get(['member_id'=>$value??0])['avatar']??'';
    	if(!empty($avatar)){
    		 //获取类型
          	$url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public' . $avatar;
          	$url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $avatar;
          	$avatar = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:$avatar);
    	}else{
    		$url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public/assets/img/avatar.png';
          	$url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public/assets/img/avatar.png';
          	$avatar = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:'');
    	}
       return $avatar;
    }
    
    public function getSignTextAttr($value,$data){
       return (new \app\index\model\Members)::get(['member_id'=>$value??0])['sign_text']??'';
    }

    public function getParentmAttr($value,$data){
    	$meObj = (new \app\index\model\Members)::field('member_id,member_id as member_nickname,member_id as member_avatar,member_id as sign_text')->get(['member_id'=>$value??0]);
       return (!empty($meObj))?$meObj->toArray():[];
    }


    public function getChildrenAttr($value,$data)
    {
    	$res = $this->where('comment_id='.$value)->field('*,member_id as member_nickname,member_id as member_avatar,member_id as sign_text,create_time as create_time_text,id as children,parent_id as parentm')->order('id DESC,create_time DESC')->select();
    	$res = $res ? $res->toArray() : [];
    	return $res;
    }

        //时间转换
   	private function formattime($time){
   		$str ='';
   		$time = is_numeric($time)?$time: strtotime($time);
   		$nowTime = time();
   		if($time>$nowTime){
   			return false;
   		}

   		$difftime = $nowTime - $time;
   		if($difftime==0){
   			$str = '刚刚';
   		} elseif($difftime<60 && $difftime>0){
   			$str = $difftime.'秒前';
   		} elseif($difftime>= 60 && $difftime<= 60 * 60){
   			$str = floor($difftime / 60).'分钟前';
   		} elseif($difftime>= 60 && $difftime <= 3600 * 24){
   			$str = floor($difftime / 3600).'小时前';
   		} else if ($difftime > 3600 * 24 && $difftime <= 3600 * 24 * 7) {
   			if (floor($difftime / (3600 * 24)) == 1) {
            	$str = "昨天";
	        } else if (floor($difftime / (3600 * 24)) == 2) {
	            $str = "前天";
	        } else {
	            $str = floor($difftime / (3600 * 24)) . '天前';
	        }
   		}elseif($difftime > 3600 * 24 * 7){
   			$str = date("Y-m-d", $time);
   		}
   		return $str;
   	}
}